package com.boang.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;  
  
import java.io.InputStream;  
  
@Service  
public class OssService {  
    @Autowired
    private OSS ossClient;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    public String uploadFile(MultipartFile file) {
        String objectName = file.getOriginalFilename(); // 可以根据需求修改文件名或路径
        try (InputStream inputStream = file.getInputStream()) {  
            ossClient.putObject(this.bucketName, objectName, inputStream);
            // 返回文件在OSS中的URL，这个URL需要根据实际情况生成  
            // 例如：https://your-bucket-name.oss-cn-hangzhou.aliyuncs.com/your-file-name  
            return "https://" + this.bucketName + "." + this.endpoint + "/" + objectName;  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
}